﻿#include "StdAfx.h"

#include "ModelToAcadR16ExportTool.h"

CModelExportTool *CreateR16Instancce()
{
	CModelExportTool* pInstance = new CModelToAcadR16ExportTool();
	return pInstance;
}

#define ACAD_EXPORT_TOOL_COMMON_IMPL CModelToAcadR16ExportTool
#define OVERWRITE_FUNCTION_StartAcad	//重写StartAcad
#define OVERWRITE_FUNCTION_GetColor		//重写GetColor
#include "AcadExportToolCommonImpl.cpp"

bool CModelToAcadR16ExportTool::StartAcad()
{
	CLSID clsid;
	CString str;
	//::CoInitialize(NULL);
	HRESULT h = ::CLSIDFromProgID(L"AutoCAD.Application", &clsid);
	if(FAILED(h))
	{
		AfxMessageBox(_T("没有安装AutoCAD软件，无法输出到AutoCAD!"));
		return false;
	}

	IUnknown * pUnknown=NULL;
	h = ::GetActiveObject(clsid, NULL, &pUnknown);//查找是否有AutoCad程序在运行
	if(FAILED(h))
	{
		AfxMessageBox(_T("请先启动AutoCAD程序!"));
		//add by dsq
		if (m_pNumInfo)
		{
			if(!m_pNumInfo->pStartBtn->IsWindowEnabled())
			{
				m_pNumInfo->pStartBtn->EnableWindow(TRUE);
				m_pNumInfo->pStopBtn->EnableWindow(FALSE);
				m_pNumInfo->pListLayer->clear();
				CWnd *pWnd;
				pWnd=CWnd::FromHandle( m_pNumInfo->hwnd);				
				pWnd->Invalidate();
				pWnd->UpdateWindow();
			}
		}
		return false;
		//h?=?m_AcadApp.CreateDispatch(clsid);??????
		//if(FAILED(h)??)???
		//{????????
		//	AfxMessageBox("AutoCAD程序打开失败!",?"警告");????
		//	return false;?????
		//}???
	}
	else
	{
		try
		{
			const GUID r16_app_id = {0x93bc4e71,0xafe7,0x4aa7,{0xbc,0x07,0xf8,0x0a,0xcd,0xb6,0x72,0xd5}};
			h=pUnknown->QueryInterface(r16_app_id,(void**)&m_pAcadApp);
			if(FAILED(h))
			{
				const GUID app_id = {0x84f323fc,0xc179,0x4704,{0x87,0xe7,0xe3,0xd5,0x76,0xc2,0x59,0x9e}};//acad2010
				h=pUnknown->QueryInterface(app_id,(void**)&m_pAcadApp);

				if( FAILED(h))
				{
					const GUID id = {0x93bc4e71,0xafe7,0x4aa7,{0xbc,0x07,0xf8,0x0a,0xcd,0xb6,0x72,0xd5}};//acad2004
					h=pUnknown->QueryInterface(id,(void**)&m_pAcadApp);

				}
			}
			pUnknown->Release();
			pUnknown=NULL;

		}
		catch(LPCTSTR lpErr)
		{
			AfxMessageBox(lpErr);
			return false;
		}
	}


	if(!m_pAcadApp)
		return false;

	//m_AcadDoc=m_AcadApp.GetActiveDocument();//可能没有当前的文档,提示出错.??????
	//m_AcadMSpace?=?m_AcadDoc.GetModelSpace();?????
	//m_pAcadApp->put_Visible(true);
	//AutoCAD::IAcadDocuments * pDocs=NULL;
	//m_pAcadApp->get_Documents(&pDocs);

	//pDocs->Release();

	//m_pAcadApp->ZoomAll();
	//CoUninitialize();
	return true;
}

AutoCAD::IAcadAcCmColor * CModelToAcadR16ExportTool::GetColor() const
{
	IDispatch *disp=NULL;
	HRESULT hr=m_pAcadApp->GetInterfaceObject(_bstr_t("AutoCAD.AcCmColor.16"), &disp);//acad2004
	if(FAILED(hr))
	{
		hr=m_pAcadApp->GetInterfaceObject(_bstr_t("AutoCAD.AcCmColor.17"), &disp);//acad2008
		if(FAILED(hr))
		{
			hr=m_pAcadApp->GetInterfaceObject(_bstr_t("AutoCAD.AcCmColor.18"), &disp);//2012
			if(FAILED(hr))
			{
				hr=m_pAcadApp->GetInterfaceObject(_bstr_t("AutoCAD.AcCmColor.19"), &disp);
				if(FAILED(hr))
				{
					hr=m_pAcadApp->GetInterfaceObject(_bstr_t("AutoCAD.AcCmColor.20"), &disp);
				}
			}
		}
	}

	if(!disp)
		return NULL;

	AutoCAD::IAcadAcCmColor *pColor=NULL;
	const GUID IID_AcCmColor_16 = {0x74adf264,0x6dd9,0x4202,{0xb6,0xdd,0xf6,0x32,0xee,0x14,0x5e,0x10}};
	disp->QueryInterface(IID_AcCmColor_16, (void **)&pColor);
	if(!pColor)
	{
		const GUID IID_AcCmColor_18 = {0xa3fe9168,0x5dea,0x45a8,{0xb4,0x7b,0x3f,0xc7,0xe8,0x40,0x7b,0x94}};//2012
		disp->QueryInterface(IID_AcCmColor_18, (void **)&pColor);
	}

	if(pColor)
		pColor->put_ColorMethod(AutoCAD::acColorMethodByRGB);
	disp->Release();

	return pColor;
}